/**
 * SingleCollection is a set of strings.
 * one can insert a string, and check if a string is in the set.
 * 
 * @author Bernhard Denner
 * @author Maximilian Csuk
 * @author Konrad Paumann
 */

package datastructures;
import java.util.HashSet;
import java.util.Iterator;

public class SingleCollection implements InsertCollection, ExistsCollection {
	
	private HashSet<String> set;
	
	public SingleCollection() {
		set = new HashSet<String>();
	}
	
	
	/**
	 * Inserts a string in the collection if the string is not 
	 * already present. 
	 */
	public void insert(String s) {
		set.add(s);
	}
	
	/**
	 * Checks if a string is in the collection.
	 * Returns 	true if the string is in the collection,
	 * 			false if the string is not in the collection
	 */
	public boolean exists(String s) {
		return set.contains(s);
	}
	
	/**
	 * Returns the size of the Collection
	 */
	public int getSize() {
		return set.size();
	}
	
	/**
	 * Returns an iterator for the strings in the collection.
	 */
	public Iterator<String> getIterator() {
		return set.iterator();
	}
	
	/**
	 * Checks for equality.
	 * two collections are equal if they have the 
	 * same set of strings.
	 * returns 	true if they have equal sets of strings
	 * 			false otherwise
	 */
	public boolean equals(SingleCollection s) {
		if (s.getSize() != set.size()) {
			return false;
		}
		Iterator<String> i = s.getIterator();
		while (i.hasNext()) {
			if (!set.contains(i.next())) {
				return false;
			}
		}
		return true;
	}

}
